package com.nf.health.app.login.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.nf.health.app.R;
import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.utils.ActivityUtils;
import com.nf.health.app.utils.PreferenceHelper;
import com.nf.health.app.utils.Util;
import com.nf.health.app.utils.ViewUtil;

/**
 * 确认新密码
 * 
 * @author jiax
 * 
 */
public class ConfirmPwdActivity extends BaseActivity implements
		OnClickListener {
	private Button btn_sucess;
	private EditText et_teleNum, et_password, et_ok_password;
	private String cruNum, verifi_code;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTitleBar(R.layout.titlebar_base);
		setContent(R.layout.activity_update_password);

		ViewUtil.setText(this, R.id.titlebar_title_tv, "确定密码");

		btn_sucess = (Button) findViewById(R.id.iv_success_submit);
		et_teleNum = (EditText) findViewById(R.id.et_teleNum);
		et_password = (EditText) findViewById(R.id.et_password);
		et_ok_password = (EditText) findViewById(R.id.et_ok_password);

		initData();
		initListener();
	}

	@SuppressLint("NewApi")
	private void initData() {
		cruNum = getIntent().getStringExtra("curNum");
		verifi_code = getIntent().getStringExtra("verifi_code");
		et_teleNum.setText(cruNum);
	}

	private void initListener() {
		btn_sucess.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.iv_success_submit:
			String phone = et_teleNum.getText().toString();
			if(cruNum.equals(phone)){
				String pwd1 = et_password.getText().toString();
				String pwd2 = et_ok_password.getText().toString();
				if (TextUtils.isEmpty(pwd1)) {
					showToast(R.string.pwd_not_blank);
					break;
				}
				if (!Util.isPassword(pwd1)) {
					showToast(R.string.pwd_style_prompt);
					break;
				}
				if (!pwd1.equals(pwd2)) {
					showToast(R.string.pwd_inconsistency);
					break;
				}
				provider.requestBackPassword(cruNum, pwd1, verifi_code, "verifycode");
			}else{
				showToast("手机号码不一致！");
			}
			break;
		}

	}

	@Override
	public void handleActionSuccess(String actionName, Object object) {
		super.handleActionSuccess(actionName, object);
		if (actionName == "verifycode") {
			String flg = (String) object;
			if (flg.equals("0")) {
				PreferenceHelper.putString(PreferenceHelper.USERNAME, cruNum);
				PreferenceHelper.putString(PreferenceHelper.PASSWORD, et_password.getText().toString());

				ActivityUtils.showActivity(this, LoginActivity.class);
				this.finish();
			} else {
				showToast("修改密码失败 ！");
			}

		}
	}
}
